package com.carlinfo.userscenter.stu.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.carlinfo.common.util.ConstantFinalUtil;
import com.carlinfo.common.util.DateUtil;
import com.carlinfo.common.util.PageInfoUtil;
import com.carlinfo.userscenter.stu.dao.IAnswerDao;
import com.carlinfo.userscenter.stu.dao.IQuestionDao;
import com.carlinfo.userscenter.stu.dao.impl.answerDaoImpl;
import com.carlinfo.userscenter.stu.dao.impl.questionDaoImpl;
import com.carlinfo.userscenter.stu.pojo.Answer;
import com.carlinfo.userscenter.stu.pojo.Question;
import com.carlinfo.userscenter.stu.pojo.Students;
import com.carlinfo.userscenter.stu.service.IQuestionService;
import com.carlinfo.userscenter.stu.service.IStudentService;

public class QuestionServiceImpl implements IQuestionService
{
	IQuestionDao questionDao = new questionDaoImpl();
	IAnswerDao answerDao = new answerDaoImpl();
	private DateUtil dateUtil = new DateUtil();
	
	IStudentService studentService = new StudentServiceImpl();

	/*-------------------------学生操作开始----------------------------------*/
	@Override
	public JSONObject saveOneQuestionService(Question question)
	{
		JSONObject jsonObj = new JSONObject();
		/* 将字符串转为json对象 */
		int res = this.questionDao.saveOneDao(question);
		if (res > 0)
		{
			jsonObj.put("code", "0");
			/* 将info中的数据取出来放到json中 */
			jsonObj.put("info",ConstantFinalUtil.INFO_JSON.get("0"));
			JSONObject dataJson = new JSONObject();
			dataJson.put("effect", res);
			dataJson.put("id", question.getId());
			jsonObj.put("data", dataJson);
		} else
		{
			jsonObj.put("code", "1");
			jsonObj.put("info", ConstantFinalUtil.INFO_JSON.get("1"));
		}
		return jsonObj;
	}

	@Override
	public List<Question> selectListQuestionService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		return this.questionDao.selectListDao(pageInfoUtil,condMap);
	}

	@Override
	public Question selectOneQuestionService(Map<String, Object> condMap)
	{
		return this.questionDao.selectOneDao(condMap);
	}

	@Override
	public JSONObject updateOneQuestionService(Question question)
	{
		JSONObject jsonObj = new JSONObject();
		/* 将字符串转为json对象 */
		int res = this.questionDao.updateOneDao(question);
		if (res > 0)
		{
			jsonObj.put("code", "0");
			/* 将info中的数据取出来放到json中 */
			jsonObj.put("info",ConstantFinalUtil.INFO_JSON.get("0"));
			JSONObject dataJson = new JSONObject();
			dataJson.put("effect", res);
			dataJson.put("id", question.getId());
			jsonObj.put("data", dataJson);
		} else
		{
			jsonObj.put("code", "1");
			jsonObj.put("info", ConstantFinalUtil.INFO_JSON.get("1"));
		}
		return jsonObj;
	}

	@Override
	public JSONObject deleteOneQuestionService(Map<String, Object> condMap)
	{
		JSONObject jsonObj = new JSONObject();
		/* 将字符串转为json对象 */
		int res = this.questionDao.deleteOneDao(condMap);
		if (res > 0)
		{
			jsonObj.put("code", "0");
			/* 将info中的数据取出来放到json中 */
			jsonObj.put("info",ConstantFinalUtil.INFO_JSON.get("0"));
			JSONObject dataJson = new JSONObject();
			dataJson.put("effect", res);
			jsonObj.put("data", dataJson);
		} else
		{
			jsonObj.put("code", "1");
			jsonObj.put("info", ConstantFinalUtil.INFO_JSON.get("1"));
		}
		return jsonObj;
	}
	/*-------------------------学生操作结束----------------------------------*/
	/*-------------------------皇上操作开始----------------------------------*/


	@Override
	public JSONObject saveOneAnswerService(Answer answer)
	{
		
		JSONObject jsonObj = new JSONObject();
		/* 将字符串转为json对象 */
		int res = this.answerDao.saveOneDao(answer);
		
		System.out.println("我保存了一条答案啊小伙子");

		System.out.println("成功保存答案,id:"+res);
		/**
		 * 存储答案的时候，加上问题的关联关系
		 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", answer.getQuestionId());
		Question question = questionDao.selectOneDao(condMap);
		List<Answer> selectList = this.answerDao.selectListDao(null, condMap);
		int count = 0;
		for (Answer answer2 : selectList)
		{
			if (answer.getQuestionId() == answer2.getQuestionId())
			{
				count++;
			}
		}
		/**
		 * 将人数给问题
		 */
		question.setPeople(count);
		this.questionDao.updateOneDao(question);
		/**
		 * 对学生的pojo进行操作
		 */
		condMap.clear();
		condMap.put("id", answer.getStuId());
		Students students = this.studentService.selectOneStudentsService(condMap);
		/**
		 * 判断时间是否是昨天，如果是，就先将昨天的清零
		 */
		if(dateUtil.isYesterday(new Date(),students.getUpdateTime())) {
			students.setTodayAccuracy(0);
			students.setTodayTrueCount(0);
			students.setTodayFalseCount(0);
			students.setTodayCount(0);
			System.out.println("我将昨天的清零了");
		}
		students.setTotalCount(students.getTotalCount() + 1);
		students.setTodayCount(students.getTodayCount()+1);
		if (answer.getShiFou())
		{
//			students.setScore(students.getScore()+question.getScore());
			students.setTodayTrueCount(students.getTodayTrueCount()+1);
			students.setCountTrue(students.getCountTrue() + 1);
		} else
		{
			students.setTodayFalseCount(students.getTodayFalseCount()+1);
			students.setCountFalse(students.getCountFalse()+1);
		}
		/**
		 * 正确率
		 */
		students.setAccuracy(Double.valueOf(students.getCountTrue())/students.getTotalCount());
		students.setTodayAccuracy(Double.valueOf(students.getTodayTrueCount())/students.getTodayCount());
		students.setUpdateTime(new Date());
		this.studentService.updateOneStudentsService(students);

		if (res > 0)
		{
			jsonObj.put("code", "0");
			/* 将info中的数据取出来放到json中 */
			jsonObj.put("info",ConstantFinalUtil.INFO_JSON.get("0"));
			JSONObject dataJson = new JSONObject();
			dataJson.put("effect", res);
			dataJson.put("id", answer.getId());
			jsonObj.put("data", dataJson);
		} else
		{
			jsonObj.put("code", "1");
			jsonObj.put("info", ConstantFinalUtil.INFO_JSON.get("1"));
		}
		return jsonObj;
	}

	@Override
	public List<Answer> selectListAnswerService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<Answer> list = this.answerDao.selectListDao(pageInfoUtil, condMap);
		for (Answer answer : list)
		{
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", answer.getQuestionId());
			Question question = this.questionDao.selectOneDao(map);
			answer.setQuestion(question);
			map.clear();
			map.put("id", answer.getStuId());
			Students students =this.studentService.selectOneStudentsService(map);
			answer.setStudents(students);
		}
		return list;
	}

	@Override
	public Answer selectOneAnswerService(Map<String, Object> condMap)
	{
		Answer answer =this.answerDao.selectOneDao(condMap);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", answer.getQuestionId());
		Question question = this.questionDao.selectOneDao(map);
		answer.setQuestion(question);
		map.put("id", answer.getStuId());
		Students students =this.studentService.selectOneStudentsService(map);
		answer.setStudents(students);
		System.out.println("我是:"+students.getName());
		return answer;
	}

	@Override
	public JSONObject updateOneAnswerService(Answer answer)
	{
		JSONObject jsonObj = new JSONObject();
		/* 将字符串转为json对象 */
		int res = this.answerDao.updateOneDao(answer);
		if (res > 0)
		{
			jsonObj.put("code", "0");
			/* 将info中的数据取出来放到json中 */
			jsonObj.put("info",ConstantFinalUtil.INFO_JSON.get("0"));
			JSONObject dataJson = new JSONObject();
			dataJson.put("effect", res);
			dataJson.put("id", answer.getId());
			jsonObj.put("data", dataJson);
		} else
		{
			jsonObj.put("code", "1");
			jsonObj.put("info", ConstantFinalUtil.INFO_JSON.get("1"));
		}
		return jsonObj;
	}
	
	@Override
	public JSONObject deleteOneAnswerService(Map<String, Object> condMap)
	{
		JSONObject jsonObj = new JSONObject();
		/* 将字符串转为json对象 */
		int res = this.answerDao.deleteOneDao(condMap);
		if (res > 0)
		{
			jsonObj.put("code", "0");
			/* 将info中的数据取出来放到json中 */
			jsonObj.put("info",ConstantFinalUtil.INFO_JSON.get("0"));
			JSONObject dataJson = new JSONObject();
			dataJson.put("effect", res);
			jsonObj.put("data", dataJson);
		} else
		{
			jsonObj.put("code", "1");
			jsonObj.put("info", ConstantFinalUtil.INFO_JSON.get("1"));
		}
		return jsonObj;
	}
	/*-------------------------皇上操作结束----------------------------------*/

	
}
